/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.service;

import com.lczyfz.istep.common.constants.DeleteFlag;
import com.lczyfz.istep.common.constants.StudentRole;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.common.utils.CacheUtils;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.sys.dao.RoleDao;
import com.lczyfz.istep.modules.sys.entity.User;
import com.lczyfz.istep.modules.sys.service.SystemService;
import com.lczyfz.istep.modules.sys.utils.UserUtils;
import com.lczyfz.istep.modules.teach.dao.TeachStudentCoursewareDao;
import com.lczyfz.istep.modules.teach.dao.TeachStudentDao;
import com.lczyfz.istep.modules.teach.entity.TeachClass;
import com.lczyfz.istep.modules.teach.entity.TeachStudent;
import com.lczyfz.istep.modules.teach.entity.TeachStudentCourseware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学生管理Service
 * @author cqh
 * @version 2017-01-14
 */
@Service
@Transactional(readOnly = true)
public class TeachStudentService extends CrudService<TeachStudentDao, TeachStudent> {

	@Autowired
	TeachStudentDao teachStudentDao ;
	@Autowired
	RoleDao roleDao;
	@Autowired
	SystemService systemService;
	@Autowired
	TeachStudentCoursewareDao teachStudentCoursewareDao;

	public TeachStudent get(String id) {
		return super.get(id);
	}
	
	public List<TeachStudent> findList(TeachStudent teachStudent) {
		return super.findList(teachStudent);
	}
	
	public Page<TeachStudent> findPage(Page<TeachStudent> page, TeachStudent teachStudent) {
		return super.findPage(page, teachStudent);
	}

	@Transactional(readOnly = false)
	public void save(TeachStudent teachStudent) {
		super.save(teachStudent);
	}
	
	@Transactional(readOnly = false)
	public void delete(TeachStudent teachStudent) {
		teachStudentDao.delete(teachStudent);
		//删除用户和角色关联表数据-真删除
		teachStudentDao.studentDeleteUserRole(teachStudent.getId());
		//删除班级学生关联表数据-真删除
		teachStudentDao.studentDeleteClassStudent(teachStudent.getId());

		// 同步到Activiti
		User user = new User();
		user.setCompany(null);
		user.setOffice(null);
		user.setLoginName(teachStudent.getLoginName());
		user.setPassword(teachStudent.getPassword());
		user.setNo(teachStudent.getNo());
		user.setName(teachStudent.getName());
		user.setEmail(teachStudent.getEmail());
		user.setPhone(teachStudent.getPhone());
		user.setMobile(teachStudent.getMobile());
		user.setUserType(teachStudent.getUserType());
		user.setLoginIp(teachStudent.getLoginIp());
		user.setLoginDate(teachStudent.getLoginDate());
		user.setLoginFlag(teachStudent.getLoginFlag());
		user.setPhoto(teachStudent.getPhoto());

		user.setOldLoginName(teachStudent.getOldLoginName());
		user.setNewPassword(teachStudent.getNewPassword());

		user.setOldLoginIp(teachStudent.getLoginIp());
		user.setOldLoginDate(teachStudent.getLoginDate());

		user.setRole(teachStudent.getRole());
		user.setRoleList(teachStudent.getRoleList());
		systemService.deleteActivitiUser(user);
		// 清除用户缓存
		UserUtils.clearCache(user);
//		// 清除权限缓存
//		systemRealm.clearAllCachedAuthorizationInfo();
	}

	@Transactional(readOnly = false)
	public int update(TeachStudent teachStudent){
		return teachStudentDao.update(teachStudent);
	}

	//TODO 该方法未被用到
	//添加、修改学生
	@Transactional(readOnly = false)
	public void saveStudent(TeachStudent teachStudent){
		if(StringUtils.isBlank(teachStudent.getId())){
			teachStudent.preInsert();
			teachStudentDao.insert(teachStudent);
			//插入用户和角色关联表数据
//			teachStudentDao.studentInsertUserRole(teachStudent.getId(), systemService.getRoleByEnname(StudentRole.student).getId());
			//插入班级学生关联表数据
//			teachStudentDao.studentInsertClassStudent(teachStudent.getId(), teachStudent.getClassId());
		}else {
			// 清除原用户机构用户缓存
			TeachStudent oldUser = teachStudentDao.get(teachStudent.getId());
			if (oldUser.getOffice() != null && oldUser.getOffice().getId() != null) {
				CacheUtils.remove(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + oldUser.getOffice().getId());
			}
			teachStudent.preUpdate();
			teachStudentDao.update(teachStudent);
			//修改班级学生关联表数据
			//teachStudentDao.studentUpdateClassStudent(teachStudent.getId(), teachStudent.getClassId());
		}
	}

	//通过Excel批量添加学生
	@Transactional(readOnly = false)
	public void saveStudentByExcel(TeachStudent teachStudent, String classId){
		teachStudent.preInsert();
		teachStudentDao.insert(teachStudent);
		//插入用户和角色关联表数据
		teachStudentDao.studentInsertUserRole(teachStudent.getId(), systemService.getRoleByEnname(StudentRole.student).getId());
		//插入班级学生关联表数据
		teachStudentDao.studentInsertClassStudent(teachStudent.getId(), classId);
	}
	//删除班级学生关联表数据
	@Transactional(readOnly = false)
	public void studentDeleteClassStudent(String userId){
		teachStudentDao.studentDeleteClassStudent(userId);
	}

	//插入班级学生关联表数据
	@Transactional(readOnly = false)
	public void studentInsertClassStudent(String userId, String classId){
		teachStudentDao.studentInsertClassStudent(userId, classId);
	}

	//获取所有班级名
	public List<TeachClass> findAllClassList(){
		return teachStudentDao.findAllClassList(DeleteFlag.deleteFlag);
	};

	//批量添加到班级-根据班级id联动获取不在该班级内的学生
	public List<TeachStudent> findBatchAddStudentByClassId(String classId){
		return teachStudentDao.findBatchAddStudentByClassId(classId, DeleteFlag.deleteFlag);
	};

	//获取所有登录名
	public List<TeachStudent> findAllLoginName(){
		return teachStudentDao.findAllLoginName(DeleteFlag.deleteFlag);
	}

	//根据登录名获取学生记录
	public TeachStudent findStudentByLoginName(String loginName) {
		return teachStudentDao.findStudentByLoginName(loginName,DeleteFlag.deleteFlag);
	}

	//根据班级id找到属于该班级下的所有学生
	public List<TeachStudent> findStudentByClassId(String classId){
		return teachStudentDao.findStudentByClassId(classId, DeleteFlag.deleteFlag);
	};

	/**
	 * AlisaYe
	 * 根据班级id查询学生列表
	 * @param page
	 * @param teachStudent
	 * @return
	 */
	public Page<TeachStudent> findPageByClassId(Page<TeachStudent> page, TeachStudent teachStudent) {
		teachStudent.setPage(page);
		page.setList(dao.findListByClassId(teachStudent));
		return page;
	}
	public List<TeachStudent> findAllStudentList(){
		return teachStudentDao.findAllStudentList(DeleteFlag.deleteFlag);
	}


	//根据学生id、课件id获取课件进度记录
	public TeachStudentCourseware findTeachStudentCourseware(String CoursewareId) {
		TeachStudentCourseware teachStudentCourseware = new TeachStudentCourseware();
		teachStudentCourseware.setUserId(UserUtils.getUser().getId());
		teachStudentCourseware.setCoursewareId(CoursewareId);
		return teachStudentCoursewareDao.get(teachStudentCourseware);
	}

	//插入-修改学生课件关联表数据
	@Transactional(readOnly = false)
	public void LearnProgress(int LearnProgress, String CoursewareId){
		TeachStudentCourseware teachStudentCourseware = new TeachStudentCourseware();
		teachStudentCourseware.setUserId(UserUtils.getUser().getId());
		teachStudentCourseware.setCoursewareId(CoursewareId);
		teachStudentCourseware.setLearnProgress(LearnProgress);
		logger.debug("==Evan==LearnProgressService=="+LearnProgress+"   ,   "+CoursewareId+"   ,   "+UserUtils.getUser().getId());
		if(teachStudentCoursewareDao.get(teachStudentCourseware) == null){
			teachStudentCourseware.preInsert();
			teachStudentCoursewareDao.insertStudentCourseware(teachStudentCourseware);
		}else {
			teachStudentCourseware.preUpdate();
			teachStudentCoursewareDao.updateStudentCourseware(teachStudentCourseware);
		}
	}

}